<template>
  <div class="table-box">
    <ProTable ref="proTable" title="会员分类列表" row-key="id" :indent="20" :columns="columns" :request-api="getUserCategoryList">
      <template #tableHeader>
        <el-button v-auth="'add'" type="primary" :icon="CirclePlus" @click="openDialog('新增', initData)">
          新增会员分类
        </el-button>
      </template>
      <template #operation="scope">
        <el-button v-auth="'view'" type="primary" link :icon="View" @click="openDialog('查看', scope.row)"> 查看 </el-button>
        <el-button v-auth="'edit'" type="primary" link :icon="EditPen" @click="openDialog('编辑', scope.row)"> 编辑 </el-button>
        <el-button v-auth="'delete'" type="primary" link :icon="Delete" @click="deleteCategory(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <UserCategoryDialog ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="category">
import { ref, reactive } from "vue";
import { Member } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { getUserCategoryList, createUserCategory, editUserCategory, deleteUserCategory } from "@/api/modules/member";
import UserCategoryDialog from "@/views/member/components/UserCategoryDialog.vue";
import { CirclePlus, EditPen, Delete, View } from "@element-plus/icons-vue";

const proTable = ref<ProTableInstance>();
// 新建默认数据
const initData = reactive({
  isShow: true
});

const columns: ColumnProps<Member.ResMemberGradeList>[] = [
  { type: "index", label: "序号", width: 80 },
  { sortable: true, prop: "name", label: "分类名称", align: "left", search: { el: "input" } },
  { prop: "operation", label: "操作", width: 220, fixed: "right" }
];

const deleteCategory = async (params: Member.ResMemberGradeList) => {
  await useHandleData(deleteUserCategory, { id: params.id }, `删除【${params.name}】会员分类`);
  proTable.value?.getTableList();
};

const dialogRef = ref<InstanceType<typeof UserCategoryDialog> | null>(null);
const openDialog = (title: string, row: Partial<Member.ResMemberGradeList> = {}) => {
  const params = {
    title,
    row: { ...row },
    isView: title === "查看",
    api: title === "新增" ? createUserCategory : title === "编辑" ? editUserCategory : undefined,
    getTableList: proTable.value?.getTableList
  };
  dialogRef.value?.acceptParams(params);
};
</script>
